<template>
    <div>
        <!--会员等级-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/customer' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>角色管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="levelFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="levelFormData.levname" placeholder="请输入等级名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary"  @click="initLevelTable">查询</el-button>
                    <el-button type="primary" @click="tianjia">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">

                    <el-table-column
                            prop="levname"
                            label="等级名称"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="growthPoint"
                            label="成长值"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="defaultStatus"
                            label="默认等级"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.defaultStatus == 0">是</span>
                            <span v-if="scope.row.defaultStatus == 1">否</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="freeFreightPoint"
                            label="免运费标准"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.freeFreightPoint == 0">免运费</span>
                            <span v-if="scope.row.freeFreightPoint == 1">不免运费</span>
                        </template>
                    </el-table-column>

                    <el-table-column
                            prop="priviledgeComment"
                            label="评论获奖励特权"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.priviledgeComment == 0">是</span>
                            <span v-if="scope.row.priviledgeComment == 1">否</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="priviledgePromotion"
                            label="专享活动特权"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.priviledgePromotion == 0">是</span>
                            <span v-if="scope.row.priviledgePromotion == 1">否</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="priviledgeMemberPrice"
                            label="会员价格特权"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.priviledgeMemberPrice == 0">是</span>
                            <span v-if="scope.row.priviledgeMemberPrice == 1">否</span>
                        </template>
                    </el-table-column>

                    <el-table-column
                            prop="priviledgeBirthday"
                            label="生日特权"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.priviledgeBirthday == 0">是</span>
                            <span v-if="scope.row.priviledgeBirthday == 1">否</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="note"
                            label="备注"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="ratio"
                            label="等级比"
                            align="center">
<!--                        <template slot-scope="scope">-->
<!--                            <span v-if="scope.row.ratio == 0">是</span>-->
<!--                            <span v-if="scope.row.ratio == 1">否</span>-->
<!--                        </template>-->
                    </el-table-column>

                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="updateLevel(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteLevel(scope.row.id)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
        <!--修改的对话框-->

        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="30%"
                @close="qx"
        >
            <!--model:表单数据-->
            <el-form  label-width="80px" :model="levelForm" :label-position="labelPosition" ref="levelForm" :rules="ruleForm">
                <el-form-item label="等级名称" prop="levname">
                    <el-input v-model="levelForm.levname" placeholder="请输入等级名称"></el-input>
                </el-form-item>

                <el-form-item label="成长值" prop="growthPoint">
                    <el-input v-model="levelForm.growthPoint" placeholder="请输入成长值"></el-input>
                </el-form-item>

                <el-form-item label="默认等级" prop="defaultStatus">
                    <el-select v-model="levelForm.defaultStatus" placeholder="请选择是否为默认等级">
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="免运费标准" prop="freeFreightPoint">
                    <el-select v-model="levelForm.freeFreightPoint" placeholder="请选择免运费标准">
                        <el-option label="免运费" value="0"></el-option>
                        <el-option label="不免运费" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="评论获奖励特权" prop="priviledgeComment">
                    <el-select v-model="levelForm.priviledgeComment" placeholder="请选择是否评论获奖励特权">
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="专享活动特权" prop="priviledgePromotion">
                    <el-select v-model="levelForm.priviledgePromotion" placeholder="请选择是否专享活动特权">
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="会员价格特权" prop="priviledgeMemberPrice">
                    <el-select v-model="levelForm.priviledgeMemberPrice" placeholder="请选择是否享有会员价格特权">
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="生日特权" prop="priviledgeBirthday">
                    <el-select v-model="levelForm.priviledgeBirthday" placeholder="请选择是否享有生日特权">
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="备注" prop="note">
                    <el-input v-model="levelForm.note" placeholder="请输入备注"></el-input>
                </el-form-item>

                <el-form-item label="等级比" prop="ratio">
                    <el-input v-model="levelForm.ratio" placeholder="请输入等级比"></el-input>
                   <!-- <el-select v-model="levelForm.ratio" placeholder="请选择等级比">
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>-->
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="qx">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Level",
        data(){
            let varName = (rule, value, callback) => {
                //查询用户名是否重复
                this.$http.post(`/client/lev/selName`,this.levelForm).then(function (res) {
                    if (res && res.data.result.code == '0') {

                        callback()
                    } else if (res && res.data.result.code == '1') {
                        callback(new Error('等级名称已存在'))
                    }

                })
            }
            return{
                //修改框内字段名左对齐
                labelPosition: 'left',
                //条件搜索对象
                levelFormData:{},
                //表格数据
                tableData:[],
                //添加修改弹出框显示
                editDialogVisible:false,
                //添加表单信息
                levelForm:{},
                //当前的显示的页码
                currentPage:1,
                //每页显示的条数
                pageSize:5,
                //总条数
                total:0,
                //表单校验数据:
                ruleForm:{
                    levname:[
                        { required: true, message: '请输入等级名称', trigger: 'blur' },
                        { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' },
                        { validator: varName, trigger: 'blur' }
                    ],
                },
            }
        },
        created() {
            //获取客户的信息
            this.initLevelTable();
        },
        methods:{
            //分页查询
            initLevelTable(){
                //get方式不支持json数据
                var that=this;
                this.$http.post(`/client/lev/initLevelTable/${this.currentPage}/${this.pageSize}`,this.levelFormData).then(function(resp){
                    console.log(resp);
                    that.tableData=resp.data.result.records;
                    that.total=resp.data.result.total;
                });

            },
            //添加
            tianjia(){
                this.editDialogVisible=true;
               // this.levelForm={};
            },
            qx(){
                this.$refs.levelForm.resetFields();
                this.editDialogVisible=false;
            },
            //删除
            deleteLevel(id){
                var that=this;

                this.$http.delete(`/client/lev/deleteLevel/${id}`).then(function (resp) {
                    that.$message.success(resp.data.message);
                    that.initLevelTable();
                    // location.reload();
                })
                this.currentPage=1;
            },
            //修改的回显
            updateLevel(id){
                this.editDialogVisible=true;
                var that=this;
                this.$http.get(`/client/lev/findElvelById/${id}`).then(function(resp){
                    console.log(resp);
                    that.levelForm=resp.data.result.level;
                })
            },
            //提交
            submit(){
                var that=this;
                this.$refs.levelForm.validate((valid)=>{
                    if(valid) {//valid表示校验通过
                        //进行id判断,如果id不为空则回显数据,走修改,如果id为空,则走添加

                        if(this.levelForm.id!=null){
                            //JSON对象
                            //修改提交
                            this.$http.put(`/client/lev/updateLevel`,this.levelForm).then(function(resp){
                                console.log(resp)
                                if(resp.data.success==true){
                                    that.editDialogVisible=false;
                                    that.$message.success(resp.data.message);
                                    that.initLevelTable();
                                }
                            })
                        }else{
                            //添加提交
                            this.$http.post(`/client/lev/insertLevel`,this.levelForm).then(function (resp) {
                                if(resp.data.success==true){
                                    that.editDialogVisible=false;
                                    that.$message.success(resp.data.message);
                                    that.initLevelTable();
                                }
                            })
                        }
                        //that.levelForm={};
                    }
                })
            },
            //当改变没有显示的条数时触发该方法
            handleSizeChange(v){
                this.pageSize=v;
                this.initLevelTable();
            },
            //切换页码时触发该方法
            handleCurrentChange(v){
                this.currentPage=v;
                this.initLevelTable();
            },

        },
    }
</script>

<style scoped>

</style>

